package com.hhj.util;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

//数据库连接工具类
public class JDBCUtil {

    private static String driver;//驱动
    private static String url;//数据库连接地址
    private static String userName;//用户名
    private static String password;//密码

    //初始化数据
    static {
        Properties params=new Properties();
        String configFile = "dp.properties";
        InputStream is= JDBCUtil.class.getClassLoader().getResourceAsStream(configFile);
        try {
            params.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        driver=params.getProperty("driver");
        url=params.getProperty("url");
        userName=params.getProperty("username");
        password=params.getProperty("password");

    }

    //获取数据库连接
    public static Connection getConnection(){
        Connection connection = null;
        try {
            Class.forName(driver);
            connection = DriverManager.getConnection(url, userName, password);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return connection;
    }



}
